<?php
	/**
	 * 
	 */
	class AlbumAction extends CommonAction {
		public function itemlist(){
			$data = S('up_cache_'.$_SESSION['lastLoginTime']);
			$uploadfilepath = __ROOT__.'/'.C('WEB_PUBLIC_PATH').'/'.C('DIR_UPLOAD_PATH');
			if (empty($data)){
				echo "还没有上传图片";
			}else{
				$themes_path = C('TMPL_PARSE_STRING');
				$html = '<table width="98%" border="0" cellpadding="4" cellspacing="0" style="margin:0;">';
				foreach ($data AS $key=>$val){
					$filenamearr = explode('/',$val['savename']);
					$filename = $filenamearr[1];
	          		$html .= '<tr>';
	            	$html .= '		<td width="3%" style="border-bottom:1px solid #e5e5e5;">
	            						<input name="pics_keep['.$key.']" type="checkbox" class="checkbox" id="pics_keep[]" value="'.$key.'" checked="checked" />
	            						<input name="pics_folder['.$key.']" type="hidden" value="'.$filenamearr[0].'" />
	            						<input name="pics_extension['.$key.']" type="hidden" value="'.$val['extension'].'" />
	            						<input name="pics_size['.$key.']" type="hidden" value="'.$val['size'].'" />
	            					</td>
	            					<td width="97%" style="border-bottom:1px solid #e5e5e5;">
	            						<input name="pics_name['.$key.']" class="input_text" type="hidden" id="pics_name[]" value="'.$filename.'" />
	            						&nbsp;描述：<input name="pics_text['.$key.']" class="input_text" style="width:360px;" type="text" id="pics_text[]" value="'.$val['name'].'" />&nbsp;&nbsp; 
	              						<a href="javascript:deluppic('.$key.')"><img src="'.$themes_path['__THEMES__'].'/images/del.png" alt="删除" width="14" height="14" border="0" align="absmiddle" title="删除"></a>&nbsp; ';
	              	$html .= "<a href=\"javascript:;\" onclick=\"addhtml('".$filenamearr[0]."/thumb_".$filename."');return false;\" onmouseover=\"showpic(event, '".$uploadfilepath.'/'.$filenamearr[0].'/thumb_'.$filename."');\" onmouseout=\"hiddenpic();\"> ".$val['savename']."</a>";
	              					$html .= "</td></tr>";
	          		
	            }
				$html .= '<tr><td colspan="2">
	            				<ul class="do_nav">
	              					<li><a href="javascript:delalluppic()" class="delalluppic">清空并删除所有已上传图片</a></li>
	            				</ul></td></tr></table> ';
				echo $html;
			}
		}

		public function delpic(){
			$fileID = $_GET['file'];
			$fileArray = S('up_cache_'.$_SESSION['lastLoginTime']);
			$uploadfilepath = './'.C('WEB_PUBLIC_PATH').'/'.C('DIR_UPLOAD_PATH');
			
			if (!empty($fileID) && !empty($fileArray[$fileID])){
				$filearr = explode('/',$fileArray[$fileID]['savename']);
				$resize = 'resize_'.$filearr[1];
				$thumb = 'thumb_'.$filearr[1];
				$mini = 'mini_'.$filearr[1];
				$folder = $filearr[0];
				$dir = $uploadfilepath.'/'.$folder.'/';
				@unlink($dir.$resize);
				@unlink($dir.$thumb);
				@unlink($dir.$mini);
				@unlink($uploadfilepath.'/'.$fileArray[$fileID]['savename']);
				
				if ( count( $fileArray ) <= 1 ){
					unset( $_SESSION['picture_list_file'] );
				}
				unset($fileArray[$fileID]);
				
				if ($_GET['type'] == 'edit'){
					S('up_edit_cache_'.$_SESSION['lastLoginTime'],$fileArray);
				}else{
					S('up_cache_'.$_SESSION['lastLoginTime'],$fileArray);
				}
				
				//echo $fileArray[$fileID]['savename'];
			}
			
		}

		public function delalluppic(){
			$fileArray = S('up_cache_'.$_SESSION['lastLoginTime']);
			$uploadfilepath = './'.C('WEB_PUBLIC_PATH').'/'.C('DIR_UPLOAD_PATH');
			foreach ($fileArray AS $key=>$val){
				$filearr = explode('/',$fileArray[$key]['savename']);
				$resize = 'resize_'.$filearr[1];
				$thumb = 'thumb_'.$filearr[1];
				$mini = 'mini_'.$filearr[1];
				$folder = $filearr[0];
				$dir = $uploadfilepath.'/'.$folder.'/';
				@unlink($dir.$resize);
				@unlink($dir.$thumb);
				@unlink($dir.$mini);
				@unlink($uploadfilepath.'/'.$fileArray[$key]['savename']);
				
				unset($_SESSION['picture_list_file']);
				S('up_cache_'.$_SESSION['lastLoginTime'],NULL);
			}
		}
		
		public function upload(){
			//$file = $_POST['Filedata'];
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$uploadfilepath = './'.C('WEB_PUBLIC_PATH').'/'.C('DIR_UPLOAD_PATH');
			$upload->savePath = $uploadfilepath."/";
			$upload->maxSize = 3145728;
			$upload->allowExts = array('jpg','gif','png','jpeg');
			$upload->saveRule = 'uniqid';
			$upload->autoSub = true;
			$upload->subType = 'date';
			$upload->dateFormat = "Ymd";
			
			$upload->thumb = true;
			$upload->imageClassPath = '@.ORG.Image';
			/*$upload->thumbMaxWidth = C("PIC_RESIZE_WIDTH").','.C("PIC_THUMB_WIDTH").','.C("PIC_MINI_WIDTH");
			$upload->thumbMaxHeight = C("PIC_RESIZE_HEIGHT").','.C("PIC_THUMB_HEIGHT").','.C("PIC_MINI_HEIGHT");
			$upload->thumbPrefix = 'resize_,thumb_,mini_';*/
			$upload->thumbMaxWidth = C("PIC_RESIZE_WIDTH");
			$upload->thumbMaxHeight = C("PIC_RESIZE_HEIGHT");
			$upload->thumbPrefix = 'resize_';
			$upload->thumbPath = $uploadfilepath."/".date("Ymd")."/";
			
			if(!$upload->upload()){
				$err = $this->error($upload->getErrorMsg());
			}else{
				$info = $upload->getUploadFileInfo();
				import('ORG.Net.Thumb');
				$file_array_i = explode('/',$info[0]["savename"]);
				$i_file_name = $file_array_i[count($file_array_i)-1];
				//生成缩略图
				$thumb = new ThumbHandler();
				$thumb->setSrcImg( $uploadfilepath.'/'.$info[0]["savename"], 0, 0 );
				$thumb->setDstImg( $uploadfilepath.'/'.$file_array_i[count($file_array_i)-2].'/thumb_'.$i_file_name, 0, "#000000");
				$thumb->setDstImgSize( 1, C('PIC_THUMB_WIDTH'), C('PIC_THUMB_HEIGHT') );
				$thumb->createImg(95, 0);
				
				$mini = new ThumbHandler();
				$mini->setSrcImg( $uploadfilepath.'/'.$info[0]["savename"], 0, 0 );
				$mini->setDstImg( $uploadfilepath.'/'.$file_array_i[count($file_array_i)-2].'/mini_'.$i_file_name, 0, "#000000");
				$mini->setDstImgSize( 1, C('PIC_MINI_WIDTH'), C('PIC_MINI_HEIGHT') );
				$mini->createImg(95, 0);
			}
			
			//self::upCache('ADD',$info[]);
			if (!isset($_SESSION['picture_list_file'])){
				$_SESSION['picture_list_file'] = array();
			}
			if (!isset($_SESSION['picture_list_file_id'])){
				$_SESSION['picture_list_file_id'] = 1;
			}else{
				$_SESSION['picture_list_file_id']++;
			}
			$_SESSION['picture_list_file'][$_SESSION['picture_list_file_id']] = $info[0];
			
			/*$cache_list = S('up_cache');
			if (!empty($cache_list)){
				$data_list = array_merge($cache_list,$_SESSION['picture_list_file']);
			}else{
				$data_list = $_SESSION['picture_list_file'];
			}
			*/
			S('up_cache_'.$_SESSION['lastLoginTime'],$_SESSION['picture_list_file']);
		}
	}
	
?>